//
//  UnifiedNativeAdMessageListCell.m
//  SEGStandard
//
//  Created by 乔杨 on 2020/7/22.
//

#import "UnifiedNativeAdMessageListCell.h"
 
@interface UnifiedNativeAdMessageListCell ()

@property (nonatomic, strong) UILabel *viewDetailLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;

@end

@implementation UnifiedNativeAdMessageListCell
 
#pragma mark - public
- (void)setupWithUnifiedNativeAdDataObject:(GDTUnifiedNativeAdDataObject *)dataObject delegate:(id<GDTUnifiedNativeAdViewDelegate>)delegate vc:(UIViewController *)vc {
    if (!dataObject) return;
    self.adView.delegate = delegate; // adView 广告回调
    self.adView.viewController = vc; // 跳转 VC
 
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 30;
    self.adView.backgroundColor = kColor_W;
    self.adView.layer.borderColor = kColor_L4.CGColor;
    self.adView.layer.borderWidth = 0.5f;
    self.adView.layer.cornerRadius = 4;
    self.adView.layer.masksToBounds = YES;
    
    [self.adView setupWithUnifiedNativeAdObject:dataObject];
     // 注册点击视图响应事件
    [self.adView registerDataObject:dataObject clickableViews:@[self.adView,
                                                                self.adView.descLabel,
                                                                self.adView.imageView]];
    
    self.adView.descLabel.font = kFont_16;
    self.adView.descLabel.textColor = kColor_1;
    self.adView.descLabel.numberOfLines = 2;
    self.adView.descLabel.preferredMaxLayoutWidth = width - 30;
    [self.adView.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.adView).mas_offset(18);
        make.left.equalTo(self.adView).mas_offset(15);
        make.right.equalTo(self.adView).mas_offset(-15);
    }];
    [self layoutIfNeeded];

    CGFloat imageWidth = (width-30);
    CGFloat imageHeight = imageWidth/kUnifiedNativeAd_ImageRate;
    self.adView.imageView.frame = CGRectMake(15, self.adView.descLabel.bottom+17, imageWidth, imageHeight);
    self.adView.imageView.layer.masksToBounds = YES;

    self.adView.logoView.frame = CGRectMake(self.adView.imageView.right-46, self.adView.imageView.bottom-14, 46, 14);
    
    self.viewDetailLabel.frame = CGRectMake(15, self.adView.imageView.bottom, 100, 50);
    [self.adView addSubview:self.viewDetailLabel];
    
    self.arrowImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
    [self.adView addSubview:self.arrowImageView];
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.adView).offset(-15);
        make.centerY.equalTo(self.viewDetailLabel);
    }];
    
    self.adView.frame = CGRectMake(15, 0, width, self.adView.imageView.bottom+50);
}
+ (CGFloat)cellHeightWithUnifiedNativeAdDataObject:(GDTUnifiedNativeAdDataObject *)dataObject {
    if (!dataObject) return 0;
    // 如果没有缓存高度
    if (!dataObject.cacheHeight) {
        CGFloat height = 0;
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 30;
        CGFloat imageWidth = (width-30);

        // 文本高度
        if (dataObject.desc.length > 0) {
            NSDictionary *attr = @{NSFontAttributeName : kFont_16};
            CGFloat desc_H = [dataObject.desc boundingRectWithSize:CGSizeMake(imageWidth, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                            attributes:attr
                                               context:nil].size.height;
            height += (18 + desc_H + 17);
        }

        // 图片高度
        CGFloat imageHeight = imageWidth/kUnifiedNativeAd_ImageRate;
      
        // 广告总高度
        height += (imageHeight + 50);
        
        // 缓存高度
        dataObject.cacheHeight = height;
    }

    return dataObject.cacheHeight;
}

#pragma mark - UITraitEnvironment
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    self.adView.layer.borderColor = kColor_L4.CGColor;
}

#pragma mark - Getter
- (UILabel *)viewDetailLabel {
    if (!_viewDetailLabel) {
        _viewDetailLabel = [[UILabel alloc] init];
        _viewDetailLabel.textColor = kColor_1;
        _viewDetailLabel.font = kFont_15;
        _viewDetailLabel.text = @"查看详情";
    }
    return _viewDetailLabel;
}
- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
    }
    return _arrowImageView;
}


@end
